#include <nxos.h>

#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"

#include "lv_drivers/wmdrv/wmdrv.h"

#define DISP_BUF_SIZE (480 * 320)

int main(int argc, char *argv[], char * envp[])
{
    lv_init();

    if (wmdrv_init() < 0)
    {
        NX_Printf("init wmdrv failed!\n");
        return -1;
    }

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = wmdrv_flush_cb;
    disp_drv.hor_res    = wmdrv_get_width();
    disp_drv.ver_res    = wmdrv_get_height();
    lv_disp_drv_register(&disp_drv);

    /* Initialize and register a display input driver */
    lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);      /*Basic initialization*/

    lv_indev_drv_t keydev_drv;
    lv_indev_drv_init(&keydev_drv);      /*Basic initialization*/

    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = wmdrv_mouse_read_cb;   //lv_gesture_dir_t lv_indev_get_gesture_dir(const lv_indev_t * indev)
    lv_indev_drv_register(&indev_drv); 

    keydev_drv.type = LV_INDEV_TYPE_KEYPAD;
    keydev_drv.read_cb = wmdrv_keyboard_read_cb;   //lv_gesture_dir_t lv_indev_get_gesture_dir(const lv_indev_t * indev)
    lv_indev_drv_register(&keydev_drv); 

    /*Create a Demo*/
#if LV_USE_DEMO_WIDGETS
    lv_demo_widgets();
#endif

#if LV_USE_DEMO_BENCHMARK
    lv_demo_benchmark();
#endif

#if LV_USE_DEMO_STRESS
    lv_demo_stress();
#endif

#if LV_USE_DEMO_KEYPAD_AND_ENCODER
    lv_demo_keypad_encoder();
#endif

#if LV_USE_DEMO_MUSIC
    lv_demo_music();
#endif

    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) {
        while(!wmdrv_poll_event()); /* poll all event */
        lv_task_handler();
        NX_ThreadSleep(10);
    }
    return 0;
}
